﻿using MediatR;
using Microsoft.EntityFrameworkCore.Infrastructure;
using MS.BasicData.Domain.Rabbit;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.Products
{
    public class CreateProductRequestHandler : IRequestHandler<CreateProductRequestModel, ApiRestult>
    {
        private readonly IProductBaseRepository _productBaseRepository;

        public CreateProductRequestHandler(IProductBaseRepository productBaseRepository)
        {
            _productBaseRepository = productBaseRepository;
        }

        public async Task<ApiRestult> Handle(CreateProductRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Code = Restult.Success,
                Message = "溯源基本信息新增成功"
            };
            Product product = new Product
            {
                ProductNameId = request.ProductNameId,
                Production = request.Production,
                Shelflife = request.Shelflife,
                Ionlicense = request.Ionlicense,
                Enterprise = request.Enterprise,
                CoverImage = request.CoverImage
            };
            await _productBaseRepository.InsertAsync(product);
            return restult;
        }
    }

    public class CreateProductRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 产品名称Id
        /// </summary>
        public int ProductNameId { get; set; }
        /// <summary>
        /// 生产批次
        /// </summary>
        public string Production { get; set; } = string.Empty;
        /// <summary>
        /// 保质期
        /// </summary>
        public int Shelflife { get; set; }
        /// <summary>
        /// 生产许可证
        /// </summary>
        public string Ionlicense { get; set; } = string.Empty;
        /// <summary>
        /// 生产企业
        /// </summary>
        public string Enterprise { get; set; } = string.Empty;
        /// <summary>
        /// 封面图片
        /// </summary>
        public string CoverImage { get; set; } = string.Empty;
    }
}
